package com.ruoyi.hotel.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.domain.bo.HmRoomBo;
import com.ruoyi.hotel.domain.vo.HmRoomVo;
import com.ruoyi.hotel.domain.HmRoom;
import com.ruoyi.hotel.mapper.HmRoomMapper;
import com.ruoyi.hotel.service.IHmRoomService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-01
 */
@RequiredArgsConstructor
@Service
public class HmRoomServiceImpl implements IHmRoomService {

    private final HmRoomMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public HmRoomVo queryById(Long roomId){
        return baseMapper.selectVoById(roomId);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<HmRoomVo> queryPageList(HmRoomBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HmRoom> lqw = buildQueryWrapper(bo);
        Page<HmRoomVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<HmRoomVo> queryList(HmRoomBo bo) {
        LambdaQueryWrapper<HmRoom> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HmRoom> buildQueryWrapper(HmRoomBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmRoom> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getRoomType()), HmRoom::getRoomType, bo.getRoomType());
        lqw.eq(StringUtils.isNotBlank(bo.getRoomStatus()), HmRoom::getRoomStatus, bo.getRoomStatus());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(HmRoomBo bo) {
        HmRoom add = BeanUtil.toBean(bo, HmRoom.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRoomId(add.getRoomId());
        }
        return flag;
    }

    @Override
    public void insertById(HmRoom room){
        baseMapper.insert(room);
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(HmRoomBo bo) {
        HmRoom update = BeanUtil.toBean(bo, HmRoom.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HmRoom entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean checkBook(String type){
        QueryWrapper<HmRoom> qw=new QueryWrapper<>();
        qw.eq("room_status","f").eq("room_type",type);
        return baseMapper.exists(qw);
    }

    @Override
    public HmRoom book(String type){
        QueryWrapper<HmRoom> qw=new QueryWrapper<>();
        qw.eq("room_status","f").eq("room_type",type);
        //自动占领找到的第一个
        List<HmRoom> roomList=baseMapper.selectList(qw);
        HmRoom hmRoom=roomList.get(0);
        //更改房间状态
        hmRoom.setRoomStatus("o");
        baseMapper.updateById(hmRoom);

        return hmRoom;
    }

    //不能修改被顾客占领的房间
    @Override
    public Boolean setRoom(HmRoom room){
        QueryWrapper<HmRoom> qw=new QueryWrapper<>();
        qw.eq("room_id",room.getRoomId());
        HmRoom temp=baseMapper.selectOne(qw);

        if(temp.getRoomStatus().equals("o")){
            return false;
        }

        baseMapper.updateById(room);
        return true;
    }


}
